<?php
namespace app\vote\controller;
use think\Session;
class Front extends \think\Controller
{

    public function _initialize(){
        $result=db('race')->select();
        $player=db('player')->limit(6)->select();
        $judges=db('judges')->limit(4)->select();  
        $this->assign(['data'=>$result,'player'=>$player,'judges'=>$judges]);
    }

   public function front(){
        
        //调用get_qrcode方法，获取二维码图片
        $base64_img =$this-> get_qrcode();

        //调用base64_save方法，将图片保存到指定位置，并获取图片新名称
        $new_name = $this->base64_save($base64_img,'./static/votefront/code/');
        return view('',['image'=>$new_name]);
    }
        
    //生成二维码
function get_qrcode(){
	$APPID = "wxb44506f4d39c5679"; 
	$APPSECRET =  "f492338be7b0e53f20e95fcead2054c8"; 
	
	//获取access_token
	$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
	
	//缓存access_token
	
    Session::set('access_token',"");
    session::set('expires_in',0);
	$ac=Session::get('access_token');
    $ex=Session::get('expires_in');
	$ACCESS_TOKEN = "";
	if(!isset($ac) || (isset($ex) && time() > $ex)){
		$json = $this->httpRequest( $access_token );
		$json = json_decode($json,true); 
		
		$_SESSION['access_token'] = $json['access_token'];
		$_SESSION['expires_in'] = time()+7200;
		$ACCESS_TOKEN = $json["access_token"]; 
	}else{
		$ACCESS_TOKEN = $_SESSION["access_token"]; 
	}
	
	//构建请求二维码参数
	//path是扫描二维码跳转的小程序路径，可以带参数?id=xxx
	//width是二维码宽度
	$qcode ="https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=$ACCESS_TOKEN";
	$param = json_encode(array("path"=>"pages/weixiurenyuan/weixiurenyuan?","width"=>150));
	
	//POST参数
	$result = $this->httpRequest( $qcode, $param,"POST");
	
	//生成二维码
	// file_put_contents("qrcode.png", $result);
	$base64_image = "data:image/jpeg;base64,".base64_encode( $result );
	
	return $base64_image; //返回base64图片数据
}

//把请求发送到微信服务器换取二维码
function httpRequest($url, $data='', $method='GET'){
	$curl = curl_init();  
	curl_setopt($curl, CURLOPT_URL, $url);  
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);  
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);  
	curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);  
	curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);  
	curl_setopt($curl, CURLOPT_AUTOREFERER, 1);  
	if($method=='POST'){
		curl_setopt($curl, CURLOPT_POST, 1); 
		if ($data != ''){
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);  
		}
	}
	
	curl_setopt($curl, CURLOPT_TIMEOUT, 30);  
	curl_setopt($curl, CURLOPT_HEADER, 0);  
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
	$result = curl_exec($curl);  
	curl_close($curl);  
	return $result;
}

/*
 * 保存base64格式图片到指定文件夹中
 * $base64_img 图片数据
 * $path 要保存到的图片地址
 */ 
 function base64_save($base64_img,$path){
	$img_base = str_replace('data:image/jpeg;base64,', '', $base64_img);
	$img_name = time().rand(1000,9999).'.jpg'; //图片新名称
	$img_path = $path.$img_name;
	file_put_contents($img_path,base64_decode($img_base));
	
	return $img_name; //返回新名称
}

    public function detail(){
        $request=request()->param();
        $sid=$request['id'];
        $race=db('race')->where('rid',$sid)->find();
        $players=db('player')->join('playergroup','player.pid=playergroup.pid','LEFT')->where('sid',$sid)->select();
        $judges=db('judges')->where('sid',$sid)->select();
        return view('',['race'=>$race,'allplayer'=>$players,'judges'=>$judges]);
    }

    public function rank(){
        $request=request()->param();
        $sid=$request['id'];
        $race=db('race')->where('rid',$sid)->find();
        $players=db('player')->join('playergroup','player.pid=playergroup.pid','LEFT')->where('sid',$sid)->order('score DESC')->select();
        return view('',['race'=>$race,'allplayer'=>$players]);
    }

}   


